<!DOCTYPE html>
<?php

/* 
 * Copyright (c) 2012 Aalto University and University of Helsinki
 * MIT License
 * see LICENSE.txt for more information
 */

// global parameters
$ENDPOINT = "http://sparql.onki.fi/onki-light/sparql";
$graph = "http://www.yso.fi/onto/yso";

// set up EasyRdf & SPARQL endpoint
set_include_path(get_include_path() . PATH_SEPARATOR . '../lib/easyrdf/lib/');
require_once "EasyRdf.php";
$sparql = new EasyRdf_Sparql_Client($ENDPOINT);

// script parameters
//$uri = $_GET['uri'];
$uri = 'http://www.yso.fi/onto/yso/p4205';

if (!isset($uri)) {
	echo "uri parameter required";
	exit();
}


$query = <<<EOQ
CONSTRUCT {
 <$uri> ?p ?o .
 ?o rdf:type ?ot .
 ?o skos:prefLabel ?ol .
 <$uri> skos:narrower ?n .
 ?n rdf:type ?nt .
 ?n skos:prefLabel ?nl .
} WHERE {
 GRAPH <$graph> {
  <$uri> ?p ?o .
  OPTIONAL { ?o rdf:type ?ot . }
  OPTIONAL { ?o skos:prefLabel ?ol . }
  OPTIONAL { ?n skos:broader <$uri> . 
             ?n rdf:type ?nt .
             ?n skos:prefLabel ?nl . }
 }
}
EOQ;

$result = $sparql->query($query);
// echo $result->dump();

// get the EasyRdf_Resource for the concept $uri
$conc = $result->resource($uri);

?>
<html>
<head>
<meta http-equiv="Content-type" content="text/html; charset=utf-8" />
<title><?php echo $conc->label('fi'); ?></title>
</head>
<body>

	<h1>
		<?php echo $conc->label('fi'); ?>
	</h1>

	<?php

	echo "<dl>";
	// loop through properties of the concept $uri
	foreach ($conc->properties() as $prop) {
		echo "<dt>$prop</dt>";
		echo "<dd>";
		// literal properties
		foreach ($conc->allLiterals($prop) as $val) {
			echo "<div>literal: $val</div>";
		}
		// resource/object properties
		foreach ($conc->allResources($prop) as $val) {
			// different display for different types of objects
			$label = $val->label('FI');
			if ($val->is_a('skos:Concept'))
				echo "<div>concept: <a href='concept?uri=$val'>$label</a></div>";
			elseif ($val->is_a('skos:ConceptScheme')) {
				echo "<div>conceptscheme: <a href='concept?uri=$val'>$val</a></div>";
			} else {
				echo "<div>other: $val ($label)</div>";
			}
		}
		echo "</dd>";
	}
	echo "</dl>";
	?>

</body>
</html>
